package com.itheima.service.Impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.dao.MemberMapper;
import com.itheima.dao.OrderMapper;
import com.itheima.dao.OrderSettingMapper;
import com.itheima.dto.OrderDTO;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 根据id查询预约信息
     * @param id
     * @return
     */
    public OrderVO findById(Integer id) {
        OrderVO orderVO = orderMapper.findById(id);
        return orderVO;
    }

    /**
     * 提交预约
     *
     * @param orderDTO
     * @return
     */
    public Integer submitOrder(OrderDTO orderDTO) {

        String validateCode = (String)redisTemplate.opsForValue().get("order_"+orderDTO.getTelephone());

        if (validateCode != null && validateCode.equals(orderDTO.getValidateCode())){
            //删除验证码
            redisTemplate.delete("order_"+orderDTO.getTelephone());
            //判断会员是否存在，信息是否完善
            Member member = memberMapper.getByTelephone(orderDTO.getTelephone());
            if(member == null || member.getIdCard() == null || member.getName() == null || member.getSex() == null){
                member = new Member();
                member.setName(orderDTO.getName());
                member.setIdCard(orderDTO.getIdCard());
                member.setPhoneNumber(orderDTO.getTelephone());
                member.setSex(orderDTO.getSex());
                member.setRegTime(new Date());
                memberMapper.insert(member);
            }

            // 将日期字符串转换为Date类型
            Date orderDate = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                orderDate = sdf.parse(orderDTO.getOrderDate());
            } catch (Exception e) {
                e.printStackTrace();
            }

            //根据预约日期查询当天是否有剩余人数;
            OrderSetting orderSetting = orderSettingMapper.findByOrderDate(orderDate);
            if(orderSetting == null || orderSetting.getReservations() >= orderSetting.getNumber()){
                return 0;
            }

            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(orderDate);
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
            order.setSetmealId(orderDTO.getSetmealId());
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            orderMapper.insert(order);
            return order.getId();
        }else {
            return -1;
        }
    }
}
